import Request, {
	baseUrl
} from '../../util/pocky-request/index'
import {
	handlerSence
} from '@/util/sceneUtil.js'
export default {
	namespaced: true,
	state: {
		login: uni.getStorageSync('login') || false,
		token: uni.getStorageSync('token') || '',
		shareUserId: uni.getStorageSync('shareUserId') || '',
		sessionId: uni.getStorageSync('sessionId') || '',
		clientUserId: uni.getStorageSync('clientUserId') || '',
		isAdmin: uni.getStorageSync('isAdmin') || false,
		canDiscuss: uni.getStorageSync('canDiscuss') || false,
		miniProgramTitle: uni.getStorageSync('miniProgramTitle') || '云课堂',
		clientUserName: uni.getStorageSync('clientUserName') || ''
	},
	actions: {
		async loginAction(context, returnUrl) {
			//TODO :读来源缓存
			var shareUserId = this.state.user.shareUserId;

			var channelInfo = handlerSence();
			if (!channelInfo.ChannelType&&shareUserId) {
				channelInfo.ChannelType = 3;
			}
			// #ifdef H5


			var url = encodeURIComponent(window.location.href);
			if (returnUrl)
				url = encodeURIComponent(returnUrl);
			window.location.href =
				`${baseUrl}api/TokenAuth/WxH5Authenticate?returnUrl=${url}&AppId=${context.rootState.common.appid}&ShareUserId=${shareUserId}&TenantId=${context.rootState.common.tenantId}
				&ChannelType=${channelInfo.ChannelType}&ChannelAppId=${channelInfo.ChannelAppId}&ChannelCode=${channelInfo.ChannelCode}`;
			// #endif
			// #ifdef MP-WEIXIN
			var [error, loginRes] = await uni.login({
				provider: 'weixin'
			});
			if (loginRes.errMsg === "login:ok") {
				var result = await Request().request({
					url: `${baseUrl}api/TokenAuth/WeChatAuthenticatesilent`,
					method: "POST",
					allow: true,
					data: {
						authProvider: 'WeChatMiniProgramSilent',
						providerAccessCode: JSON.stringify({
							AppId: context.rootState.common.appid,
							Code: loginRes.code,
							ChannelType: channelInfo.ChannelType || 0,
							ChannelAppId: channelInfo.ChannelAppId || "",
							ChannelCode: channelInfo.ChannelCode || "",
							ShareUserId: shareUserId || ""
						}),
					}
				});
				if (result) {
					uni.setNavigationBarTitle({
						title: result.miniProgramTitle
					})
					context.commit('login', {
						token: result.accessToken,
						sessionId: result.sessionId,
						clientUserId: result.clientUserId,
						isAdmin: result.isAdmin,
						canDiscuss: result.canDiscuss,
						miniProgramTitle: result.miniProgramTitle,
						clientUserName: result.clientUserName
					});
				}
			}
			// #endif
		}


	},
	mutations: {
		login(state, userInfo) {
			uni.setStorageSync('login', true);
			state.login = true;
			if (userInfo.token) {
				uni.setStorageSync('token', userInfo.token);
				state.token = userInfo.token;
			}
			if (userInfo.sessionId) {
				uni.setStorageSync('sessionId', userInfo.sessionId);
				state.sessionId = userInfo.sessionId;
			}
			if (userInfo.clientUserId) {
				uni.setStorageSync('clientUserId', userInfo.clientUserId);
				state.clientUserId = userInfo.clientUserId;
			}
			if (userInfo.isAdmin) {
				uni.setStorageSync('isAdmin', userInfo.isAdmin);
				state.isAdmin = userInfo.isAdmin;
			}
			else{
				uni.setStorageSync('isAdmin',false);
				state.isAdmin = false;
			}
			if (userInfo.canDiscuss) {
				uni.setStorageSync('canDiscuss', userInfo.canDiscuss);
				state.canDiscuss = userInfo.canDiscuss;
			}
			else{
				uni.setStorageSync('canDiscuss', false)
				state.canDiscuss = false;
			}
			if (userInfo.miniProgramTitle) {
				uni.setStorageSync('miniProgramTitle', userInfo.miniProgramTitle);
				state.miniProgramTitle = userInfo.miniProgramTitle;
			}
			if(userInfo.clientUserName){
				uni.setStorageSync('clientUserName', userInfo.clientUserName);
				state.clientUserName = userInfo.clientUserName;
			}
		},
		logout(state) {
			console.log('登出')
			uni.setStorageSync('login', false);
			state.login = false;
			uni.removeStorageSync('token');
			state.token = '';
			uni.removeStorageSync('shareUserId');
			state.shareUserId = '';
			uni.removeStorageSync('sessionId');
			state.sessionId = '';
			uni.removeStorageSync('clientUserId');
			state.clientUserId = '';
			uni.removeStorageSync('isAdmin');
			state.isAdmin = false;
			uni.removeStorageSync('canDiscuss');
			state.canDiscuss = false;
			uni.removeStorageSync('miniProgramTitle');
			state.miniProgramTitle = '';

		},
		saveShareUserId(state, shareUserId) {
			if (shareUserId) {
				uni.setStorageSync('shareUserId', shareUserId);
				state.shareUserId = shareUserId;
			}
		},
		saveSessionId(state, sessionId) {
			uni.setStorageSync('sessionId', sessionId);
			state.sessionId = sessionId;
		},
		removeSessionId(state) {
			uni.removeStorageSync('sessionId');
			state.sessionId = '';
		}
	}
}
